//
//  WKUsernameRegisterVM.m
//  WuKongUsernameLogin
//
//  Created by tt on 2023/9/15.
//

#import "WKUsernameRegisterVM.h"
#import "WKMnemonicsVC.h"
#import "WKLoginResp.h"
#import "WKUsernameLoginVM.h"
@interface WKUsernameRegisterVM()

@property(nonatomic,strong) NSString *username;
@property(nonatomic,strong) NSString *password;
@property(nonatomic,strong) NSString *nickname;

@end

@implementation WKUsernameRegisterVM


- (NSArray<NSDictionary *> *)tableSectionMaps {
    
    __weak typeof(self) weakSelf = self;
    return @[
        @{
            @"height":@(40.0f),
            @"items":@[
                    @{
                        @"class":WKLabelModel.class,
                        @"text":LLang(@"账号注册"),
                        @"textColor": [WKApp shared].config.defaultTextColor,
                        @"font": [[WKApp shared].config appFontOfSizeSemibold:18.0f],
                        @"center":@(true),
                    },
            ],
        },
        @{
            @"height":@(40.0f),
            @"items":@[
                    @{
                        @"class":WKTextFieldItemModel.class,
                        @"placeholder":LLang(@"请填写账号"),
                        @"label": @"账号",
                        @"showBottomLine":@(true),
                        @"onChange": ^(NSString *value) {
                            weakSelf.username = value;
                        }
                    },
            ],
        },
        @{
            @"height":@(0.01f),
            @"items":@[
                    @{
                        @"class":WKTextFieldItemModel.class,
                        @"placeholder":LLang(@"请输入密码"),
                        @"label": @"密码",
                        @"showBottomLine":@(true),
                        @"password":@(true),
                        @"onChange": ^(NSString *value) {
                            weakSelf.password = value;
                        }
                    },
            ],
        },
        @{
            @"height":@(0.01f),
            @"items":@[
                    @{
                        @"class":WKTextFieldItemModel.class,
                        @"placeholder":LLang(@"请输入的昵称"),
                        @"label": @"昵称",
                        @"showBottomLine":@(true),
                        @"onChange": ^(NSString *value) {
                            weakSelf.nickname = value;
                        }
                    },
            ],
        },
        @{
            @"height":@(80.0f),
            @"items":@[
                    @{
                        @"class":WKButtonItemModel2.class,
                        @"title": LLang(@"下一步"),
                        @"width": @(150.0f),
                        @"height":@(40.0f),
                        @"onPressed":^{
                            [weakSelf registerUser];
                        }
                    },
            ],
        },
    ];
}

-(void) registerUser {
   UIView *topView = WKNavigationManager.shared.topViewController.view;
    if(!self.username || self.username.length == 0) {
        [topView showHUDWithHide:LLang(@"账号不能为空！")];
        return;
    }
    if(!self.password || self.password.length == 0) {
        [topView showHUDWithHide:LLang(@"密码不能为空！")];
        return;
    }
    if(!self.nickname || self.nickname.length == 0) {
        [topView showHUDWithHide:LLang(@"昵称不能为空！")];
        return;
    }
    [topView showHUD];
    [WKAPIClient.sharedClient POST:@"user/usernameregister" parameters:@{
        @"username": self.username,
        @"password": self.password,
        @"name": self.nickname,
    }].then(^(NSDictionary *result){
        [topView hideHud];
        
        WKLoginResp *resp = (WKLoginResp*)[WKLoginResp fromMap:result[@"data"] type:ModelMapTypeAPI];
        
        [WKUsernameLoginVM handleLoginData:resp isSave:NO];
       
        WKMnemonicsVC *vc = [WKMnemonicsVC new];
        [WKNavigationManager.shared pushViewController:vc animated:YES];
    }).catch(^(NSError *error){
        [topView switchHUDError:error.domain];
    });
    
}


@end
